def is_prime(n):
    if n < 2:
        return False
    for i in range(2, int(n**0.5)+1):
        if n % i == 0:
            return False
    return True

def largest_prime(num):
    if len(str(num)) != 4:
        return 0
    digits = [int(i) for i in str(num)]
    digits.sort(reverse=True)
    
    largest_prime_num = 0
    for i in range(4):
        for j in range(4):
            for k in range(4):
                for l in range(4):
                    if i != j and i != k and i != l and j != k and j != l and k != l:
                        current_num = digits[i]*1000 + digits[j]*100 + digits[k]*10 + digits[l]
                        if is_prime(current_num) and current_num > largest_prime_num:
                            largest_prime_num = current_num
    return largest_prime_num

num = int(input("Введите четырехзначное натуральное число: "))
result = largest_prime(num)
print(result)
